﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcContrib.ActionResults;
using Press.MVC.Data;
using Press.MVC.Services;
using System.Xml;
using System.Xml.Linq;

using Press.MVC.Web.ActionFilters;

namespace Press.MVC.Web.Controllers
{
    [Authorize(Roles = "MtaAdmin")]
    public class MtaController : BaseController
    {

        #region Constructor        
        IBlogService _blogService;

        public MtaController(IBlogService blogService) 
        {
            _blogService = blogService;
        }

        #endregion

        /// <summary>
        /// Index - Show Multi-tenant Dashboard
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            ViewData["Title"] = "Multi-Tenant Admin Home";
            ViewData["Message"] = "TODO";

            return View("Index");
        }

       

        /// <summary>
        /// BlogListBasic - Show Blog List Basic View Page /mta/BlogListBasic
        /// </summary>
        /// <returns></returns>
        public ActionResult BlogListBasic()
        {
            ViewData["Title"] = "Multi-Tenant Blog List (Advanced)";
            ViewData["Message"] = "TODO";

            BlogData bdata = new BlogData();
            bdata.Blogs = _blogService.GetBlogs();

            return View("BlogListBasic",bdata);
        }

        /// <summary>
        /// BlogListAdvanced - Show Blog List Advanced View Page
        /// </summary>
        /// <returns></returns>
        public ActionResult BlogListAdvanced()
        {
            ViewData["Title"] = "Multi-Tenant Blog List (Advanced)";
            ViewData["Message"] = "TODO";

            return View("BlogListAdvanced");
        }

        // TODO as DHTMLX has a no API to generate XML, write one that will allow the user to 
        // build the Menu bar through a developer web interface. 
        public XmlResult2 GetMtaAdminMenuXml()
        {
            string xml = @"<?xml version='1.0' ?>" + 
@" <menu maxItems=""12"" name=""Multi-Tenant Administration"" mixedImages=""yes"">" +
	@" <MenuItem name=""Global Dashboard"" src=""../../samples/images/programs.gif"" id=""main_file"" width=""120px"" panelWidth=""130px"" withoutImages=""yes"">" +
			@" <MenuItem name=""Manage Blogs"" id=""manb"" href=""/mta/BlogListAdvanced"" >" +
            @" <MenuItem name=""Add Blog"" src=""""  id=""manb_1"" href=""/mta/BlogNew"" />" +
                @" <MenuItem name=""View Blogs"" src=""""  id=""manb_2"" href=""/mta/BlogListAdvanced""/>" +
            @" </MenuItem>" +
            @" <MenuItem name=""Logout"" id=""logout"" href=""/User/Logout"" />" +
            //@" <MenuItem name=""Edit with Notepad"" id=""edit_with""/> " +
            //@" <MenuItem name=""Save As..."" id=""save""/>"  +
            //@" <divider id=""div_1""/>"  +	
            //@" <MenuItem name=""Page Setup..."" id=""page""/>" +
            //@" <MenuItem name=""Print..."" id=""print""/>" +
            //@" <MenuItem name=""Print Preview..."" id=""preview""/>" +
            //@" <divider id=""div_2""/>" +	
            //@" <MenuItem name=""Properties"" id=""properties""/>" +
            //@" <MenuItem name=""Close"" id=""close""/>" +
	@" </MenuItem>" +
    //@" <MenuItem name=""Edit"" id=""main_edit""  width=""120px"" panelWidth=""130px"">" +	
    //        @" <MenuItem name=""Undo"" src=""../../samples/images/undo.gif"" id=""edit_undo""/>" +	
    //        @" <MenuItem name=""Redo"" src=""../../samples/images/redo.gif"" id=""edit_redo"" />" +
    //        @" <divider id=""div_3""/>" +	
    //        @" <MenuItem name=""Cut"" src="""" id=""cut""/>" +	
    //        @" <MenuItem name=""Copy"" src="""" id=""copy""/>" +
    //        @" <MenuItem name=""Paste"" src=""../../samples/images/paste.gif"" id=""paste""/>" +
    //        @" <MenuItem name=""Clear all"" src="""" id=""edit_clear""/>" +
    //        @" <divider id=""div_4""/>" +	
    //        @" <MenuItem name=""Select All"" src="""" id=""all""/>" +
    //        @" <MenuItem name=""Filters"" src=""../../samples/images/icon15.gif"" id=""edit_filters""  withoutImages=""yes"">" +
    //            @" <MenuItem name=""Filter 1"" src=""""  id=""edit_filters_1""/>" +	
    //            @" <MenuItem name=""Filter 2"" src=""""  id=""edit_filters_2""/>" +	
    //            @" <MenuItem name=""Filter 3"" src=""""  id=""edit_filters_3""/>" +	
    //            @" <MenuItem name=""Filter 4"" src=""""  id=""edit_filters_4""/>" +	
    //            @" <MenuItem name=""Filter 5"" src=""""  id=""edit_filters_5""/>" +	
    //            @" <MenuItem name=""Filter 6"" src=""""  id=""edit_filters_6""/>" +	
    //            @" <MenuItem name=""Filter 7"" src=""""  id=""edit_filters_7""/>" +	
    //            @" <MenuItem name=""Filter 8"" src=""""  id=""edit_filters_8""/>" +	
    //            @" <MenuItem name=""Filter 9"" src=""""  id=""edit_filters_9""/>" +	
    //            @" <MenuItem name=""Filter 10"" src=""""  id=""edit_filters_9""/>" +					
    //        @" </MenuItem>" +
    //        @" <MenuItem name=""Find on Page..."" id=""find""/>" +
    //@" </MenuItem>" +
    //@" <MenuItem name=""Help"" id=""main_help"" width=""120px"" panelWidth=""130px"">" + 
    //        @" <MenuItem name=""Index"" width=""120px"" src=""../../samples/images/help.gif""  id=""help_index""/>" +	
    //        @" <MenuItem name=""License"" src="""" id=""help_license""/>" +
    //        @" <divider id=""div_5""/>" +	
    //        @" <MenuItem name=""About"" src=""""  id=""help_about""/>" +			
    //@" </MenuItem>" +
@" </menu>";

            

            XDocument xdoc = XDocument.Parse(xml);
            string s = xdoc.ToString();          

            return new XmlResult2(s);
            
        }      

        public XmlResult GetBlogsAdvanced()
        {
            IList<Press.MVC.Data.Blog> blogs = _blogService.GetBlogs();
            //TODO make this reusable instead of one off
            

            int i = 1;
            string subTitle = "";
            XElement xmlRoot = new XElement("rows");
           
            foreach (Press.MVC.Data.Blog item in blogs)
            {
                if (item.SubTitle != null)
                {
                    subTitle = item.SubTitle.ToString();
                }
                else
                {
                    subTitle = "(empty)";
                }
                xmlRoot.Add(new XElement("row",
                        new XAttribute("id", item.ID.ToString()),
                        new XElement("cell", i.ToString()),
                        new XElement("cell", item.Name),
                        new XElement("cell", subTitle),
                        new XElement("cell", item.IsActive.ToString()))
                );

                i++;
            }           


            return new XmlResult(xmlRoot);            
        }     


        public ActionResult BlogNew()
        {
            ViewData["Title"] = "Multi-Tenant Admin Home - Add Blog";
            ViewData["Message"] = "TODO";
            return View("BlogNew");
        }

        /// <summary>
        /// BlogEdit - Show Multi-tenant Blog Edit
        /// </summary>
        /// <returns></returns>
        public ActionResult BlogEdit(int blogid)
        {
            ViewData["Title"] = "Multi-Tenant Admin Home";
            ViewData["Message"] = "TODO";

            return View("BlogEdit");
        }

        public ActionResult BlogSave()
        {
            Blog b = new Blog();
            b.IsActive = Convert.ToBoolean(this.ReadFromRequest("Active").ToString());
            b.Name = this.ReadFromRequest("Title");
            b.ShortName = this.ReadFromRequest("ShortName");
            b.SubTitle = this.ReadFromRequest("SubTitle");
            _blogService.SaveBlog(b);
            return View("BlogListAdvanced");
        }


        //MTA will reuse pretty much all existing Blog Admin views plus MTA has ADD/UPDATE/DELETE/DISABLE BLOG/ functionality

    //        * Update/Delete Posts
    //* Add/Update/Delete Tags
    //* Update/Delete Comments
    //* Add/Update/Delete Categories
    //* Blog Settings
    //      o Specify Theme
    //* Post Settings
    //* Comment Settings

//User/Group Module

//    * Add/Update/Delete User
//    * Add/Update/Delete Groups
//    * Permissions - specify users in groups
//    * User Settings


//Error Log Module (Multi Tenant only)

//    * View error log messages


//Resource String Module (Multi Tenant only)

//    * Add/Update/Delete resource strings


//Login Settings Module (Multi Tenant only)

//    * Specify Authentication System(OpenID, Active Directory, Standard SQL User Store, ASP.NET Membership) configure forms based authentication configuration settings


//System Maintenance Module (Multi Tenant only)

//    * Enable maintenance msg.
//    * User can specify maintenance msg
//    * Option to enable login access or disable login access.


    }

    
}
